package com.comdo.newup_loan;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.EdgeEffectCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends Activity {

	private ViewPager view_pager;
	private MyAdapter adapter;
	private EdgeEffectCompat rightEdge;
	private LayoutInflater inflater;
	private ArrayList<Integer> mUrl = new ArrayList<Integer>();
	List<View> list = new ArrayList<View>();
	private ImageView[] indicator_imgs;
	private View item;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				for (int i = 0; i < 3; i++) {
					item = inflater.inflate(R.layout.indicator_item1, null);
					list.add(item);
				}
				mUrl.add(R.drawable.one);
				mUrl.add(R.drawable.two);
				mUrl.add(R.drawable.three);

				adapter.notifyDataSetChanged();
				break;

			}
		}
	};
	private SharedPreferences sp;
	private Editor editor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sp = getSharedPreferences("wash", MODE_PRIVATE);
		if (sp.getBoolean("isfirst", false)) {
			//startActivity(new Intent(WelcomeActivity.this, StartActivity.class));
			overridePendingTransition(0, 0);
			WelcomeActivity.this.finish();
		} else {
			editor = sp.edit();
			editor.putBoolean("isfirst", true);
			editor.commit();
		}
		setContentView(R.layout.activity_welcome);
		Log.e("WelcomeActivity", "onCreate");
		initView();
	}
	@Override
	protected void onDestroy() {
		
		super.onDestroy();
		Log.e("WelcomeActivity", "onDestroy");
	}

	private void initView() {
		view_pager = (ViewPager) findViewById(R.id.view_pager);
		inflater = LayoutInflater.from(this);
		adapter = new MyAdapter(list);
		view_pager.setAdapter(adapter);
		view_pager.setOnPageChangeListener(new MyListener());
		try {
			Field leftEdgeField = view_pager.getClass().getDeclaredField(
					"mLeftEdge");
			Field rightEdgeField = view_pager.getClass().getDeclaredField(
					"mRightEdge");
			if (leftEdgeField != null && rightEdgeField != null) {
				rightEdgeField.setAccessible(true);
				rightEdge = (EdgeEffectCompat) rightEdgeField.get(view_pager);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		handler.sendEmptyMessage(0);
	}

	private class MyAdapter extends PagerAdapter {

		private List<View> mList;
		private int index;

		public MyAdapter(List<View> list) {
			mList = list;

		}

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(mList.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View view = mList.get(position);
			ImageView image = (ImageView) view.findViewById(R.id.image);
			//ImageLoader.getInstance().displayImage("drawable://" +mUrl.get(position), image);
			//image.setImageDrawable(getResources().getDrawable(mUrl.get(position)));
			Glide.with(WelcomeActivity.this).load(mUrl.get(position)).into(image);
			container.removeView(mList.get(position));
			container.addView(mList.get(position));
			setIndex(position);
			if(position==2){
				image.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
					//	startActivity(new Intent(WelcomeActivity.this,
					//			StartActivity.class));
						overridePendingTransition(0, 0);
						WelcomeActivity.this.finish();
						
					}
				});
			}
			return mList.get(position);
		}
	}

	/**
	 * 动作监听器，可异步加载图片
	 * 
	 */
	private class MyListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int state) {
			if (rightEdge != null && !rightEdge.isFinished()) {// 到了最后一张并且还继续拖动，出现蓝色限制边条了
				//startActivity(new Intent(WelcomeActivity.this,
				//		StartActivity.class));
				overridePendingTransition(0, 0);
				WelcomeActivity.this.finish();
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// if (leftEdge != null && rightEdge != null) {
			// leftEdge.finish();
			// rightEdge.finish();
			// leftEdge.setSize(0, 0);
			// rightEdge.setSize(0, 0);
			// }
		}

		@Override
		public void onPageSelected(int position) {


		}

	}

}
